<?php

namespace App\Trip\Orders\BookOrders\Params\AbstractOrderParam;

use App\Trip\Enums\OrderBusiness;
use App\Trip\Enums\TripTypes;
use App\Trip\Orders\BookOrders\Params\AbstractParam\TripParam;
use App\Trip\Orders\BookOrders\Params\Plane\PlaneParam;
use App\Trip\Orders\BookOrders\Params\TrainParam;
use Exception;

abstract class AbstractOrderParam
{
    protected string $order_sn = '';
    /**
     * 1 线上出票 2 线下出票
     *
     * @var int
     */
    protected int $online = 2;

    /**
     * 子订单内容
     *
     */
    public abstract function getOrder();

    /**
     * @throws Exception
     */
    public function getSelectSeat(): array
    {
        throw new Exception("当前业务不支持选座");
    }

    /**
     * @throws Exception
     */
    public function getTripId(): string
    {
        throw new Exception("无法获取行程id");
    }

    public abstract function getOutOrderSn();

    public function getOnline(): int
    {
        return $this->online;
    }

    public function setOnline(int $online): void
    {
        $this->online = $online;
    }

    /**
     * 生成订单编号
     *
     * @return string
     * @throws Exception
     */
    public function generateOrderSn(): string
    {
        $this->order_sn = generate_order_id($this->getTripType(), OrderBusiness::ORDER);
        return $this->order_sn;
    }

    /**
     * @throws Exception
     */
    public function getTripType(): TripTypes
    {
        if (is_null(TripTypes::tryFrom($this->order_data['trip_type']))) {
            throw new Exception("出行类型错误");
        }
        return TripTypes::from($this->order_data['trip_type']);
    }

    /**
     * 火车信息|飞机信息
     * @return TripParam
     * @throws Exception
     */
    public function getTripParam(): TripParam
    {
        if ($this->getTripType()->isPlane()) {
            return new PlaneParam($this->order_data);
        } elseif ($this->getTripType()->isTrain()) {
            // 这里验证 是否是抢票 如果是抢票 需要将最大的规则写入
            return new TrainParam($this->order_data);
        }
        throw new Exception("trip type unsupported ");
    }

    /**
     * 需要子类 重载
     * @throws Exception
     */
    public function OrderService(): string
    {
        throw new Exception("请填写服务商");
    }

    public function getSeatName(): string
    {
        return $this->seat_type;
    }

    public function getTripNo(): int
    {
        return $this->trip_no;
    }

    public function getTransferFlag(): int
    {
        return $this->order_data['transfer_flag'];
    }

    /**
     * 需要子类重载
     * @return bool
     * @throws Exception
     */
    public function isSnapUp(): bool
    {
        throw new Exception("当前业务不支持抢票");
    }

    /**
     * @throws Exception
     */
    public function __get($name)
    {
        if (isset($this->order_data[$name])) {
            return $this->order_data[$name];
        } else {
            throw new Exception("AbstractOrderParam 属性：{$name}不存在");
        }
    }
}